import re
from enum import Enum

from pydantic import BaseModel, constr, Field, field_validator


class User(BaseModel):
    id: int
    username: str
    password_hash: str
    role: str
    email: str

class RoleEnum(str, Enum):
    GUEST = "游客"
    USER = "普通用户"
    ADMIN = "管理员"

class UserLogin(BaseModel):
    username: str
    password: str

class UserRegister(BaseModel):
    username: constr(min_length=2, max_length=20)
    password: constr(min_length=8)
    role: str = Field(default=RoleEnum.USER.value, description="用户角色")
    email: str

    @field_validator("email")
    def email_format(cls, v: str) -> str:
        # 限制一下邮箱格式
        allowed_domains = ["qq.com", "163.com", "126.com", "sina.com", "aliyun.com", "gmail.com", "outlook.com", "yahoo.com", "hotmail.com"]
        pattern = r"^[a-zA-Z0-9_.+-]+@(" + "|".join(allowed_domains) + ")$"
        if not re.match(pattern, v):
            raise ValueError("邮箱格式错误")
        return v


class AdminRegister(UserRegister):
    role: str = Field(default=RoleEnum.ADMIN.value, description="用户角色")